package com.momoauth.common.tps.aliyun.service;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.momoauth.common.tps.aliyun.utils.AliPayUtil;

import javax.servlet.http.HttpServletResponse;

public class AliPayService {

    private String serverUrl;
    private String appId;
    private String privateKey;
    private String format;
    private String charset;
    private String alipayPublicKey;
    private String signType;
    private String notify_url;
    private String return_url;
    private String product_code;
    private String subject;
    private String body;

    public AliPayService(
            String serverUrl,
            String appId,
            String privateKey,
            String format,
            String charset,
            String alipayPublicKey,
            String signType,
            String notify_url,
            String return_url,
            String product_code,
            String subject,
            String body
    ) {
        this.serverUrl = serverUrl;
        this.appId = appId;
        this.privateKey = privateKey;
        this.format = format;
        this.charset = charset;
        this.alipayPublicKey = alipayPublicKey;
        this.signType = signType;
        this.notify_url = notify_url;
        this.return_url = return_url;
        this.product_code = product_code;
        this.subject = subject;
        this.body = body;
    }

    public void alipay(HttpServletResponse response, String out_trade_no, String total_amount) {
        AlipayClient alipay_client = new DefaultAlipayClient(
                serverUrl,
                appId,
                privateKey,
                format,
                charset,
                alipayPublicKey,
                signType);
        AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);
        AliPayUtil.alipay(
                alipay_client,
                alipay_request,
                response,
                out_trade_no,
                total_amount,
                product_code,
                subject,
                body
        );
    }
}
